package com.yeqiangwei.commons.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yeqiangwei.commons.util.Pagination.Order;

public class Parameter {

	private Pagination pagination;

	private List<OrderBy> orderByList;

	private Map<String, Object> eqMap;

	private String distinct;
	
	public static Parameter getDefault(Pagination pagination){
		Parameter param = new Parameter();
		param.setOrderBy(new Object[]{
				Parameter.getOrderBy("id", false)
		});
		if(pagination != null){
			param.setPagination(pagination);
		}
		return param;
	}

	public Map<String, Object> getEqMap() {
		return eqMap;
	}

	public void setEqMap(Map<String, Object> eqMap) {
		this.eqMap = eqMap;
	}

	public void setProperty(String key, Object value) {
		if (eqMap == null) {
			eqMap = new HashMap<String, Object>();
		}
		eqMap.put(key, value);
	}

	public static void main(String args[]) {

		/*
		 * System.out.println(Parameter.CommonOrderBy.desc.getOrder());
		 * System.out.println(Parameter.CommonOrderBy.desc.getOrderProperty());
		 * Parameter param = new Parameter(); Map<String, Object> map = new
		 * HashMap<String, Object>(); map.put("id", 12312); map.put("name",
		 * "abc"); map.put("namec", "ddddbc"); param.setCriterionList(new
		 * Object[]{ Parameter.criteriaEq("id", 1) ,Parameter.criteriaEq("id",
		 * 1) ,Parameter.criteriaNotEq("id", 12) ,Parameter.criteriaLike("id",
		 * "%abc%") , Parameter.criteriaOr(Parameter.criteriaAnd(
		 * Parameter.criteriaNotEq("id", 12) ,Parameter.criteriaEq("id", 11) ) ,
		 * Parameter.criteriaEq("name","abc")) Parameter.criteriaAnd(
		 * Parameter.criteriaNotEq("id", 12) ,Parameter.criteriaEq("id", 11))
		 * 
		 * , Parameter.criteriaAnd( Parameter.criteriaNotEq("id", 12)
		 * ,Parameter.criteriaEq("id", 11)) Parameter.criteriaAllEq(map) });
		 * System.out.println(param.getCriterionList()); for (Criterion
		 * criterion : param.getCriterionList()){
		 * System.out.println(criterion.toString());
		 * 
		 * }
		 */
		/*
		 * System.out.println(Parameter.CommonOrderBy.object.getOrderBy("orderBy"
		 * , false));
		 */

		/*
		 * System.out.println(Parameter.CommonOrderBy.asc.getOrder());
		 * System.out.println(Parameter.OrderByImpl.);
		 */

	}

	public static OrderBy getOrderBy(final String propertyName,
			final SortType sortType) 
	{
		if(sortType == SortType.asc){
			return new OrderByImpl(propertyName, true);
		}
		else {
			return new OrderByImpl(propertyName, false);
		}
	}
	
	public static OrderBy getOrderBy(String propertyName, boolean asc) {
		return new OrderByImpl(propertyName, asc);
	}

	public static enum SortType {
		asc, desc;
	}

	public interface OrderBy {
		public Order getOrder();

		public String getString(String aliasName);

		public String getOrderProperty();

		public boolean getAsc();
	}

	public static class OrderByImpl implements OrderBy {

		private String propertyName;

		private boolean asc = true;

		private Order order;

		protected OrderByImpl() {
			/* this.order.getClass(); */
		}

		public OrderByImpl(String propertyName, boolean asc) {
			this.propertyName = propertyName;
			this.asc = asc;
		}

		public boolean getAsc() {
			return asc;
		}

		public void setAsc(boolean asc) {
			this.asc = asc;
		}

		@Override
		public Order getOrder() {
			return this.order;
		}

		@Override
		public String getOrderProperty() {
			return propertyName;
		}

		@Override
		public String getString(String aliasName) {

			return null;
		}

	}

	public void setOrderBy(Object[] objs) {
		if(this.orderByList == null){
			this.orderByList = new ArrayList<OrderBy>();
		}
		if(!ValidatorHelper.isEmpty(objs)){
			for(Object obj : objs){
				this.orderByList.add((OrderBy) obj);
			}
		}
	}

	public List<OrderBy> getOrderByList() {
		return orderByList;
	}

	public void setOrderByList(List<OrderBy> orderByList) {
		this.orderByList = orderByList;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public String getDistinct() {
		return distinct;
	}

	public void setDistinct(String distinct) {
		this.distinct = distinct;
	}

}

